package cn.kingyang.yiyi.retrofit.helper;

import com.franmontiel.persistentcookiejar.ClearableCookieJar;
import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.cache.SetCookieCache;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;

import java.util.concurrent.TimeUnit;

import cn.kingyang.yiyi.app.App;
import okhttp3.OkHttpClient;

/**
 * Created by 饭团 on 2017/1/17.
 */

public class OkHttpClientHelper {
//    DataBaseOpenHelper db=DataBaseOpenHelper.getInstance(App.getContext(),"cookie",1,DataBaseOpenHelper.getTables());
    ClearableCookieJar cookieJar =
            new PersistentCookieJar(new SetCookieCache(), new SharedPrefsCookiePersistor(App.getContext()));
    private OkHttpClient mClient;
    private final  static  long TIMEOUT = 6000;  //超时时间

    private OkHttpClientHelper(){

    }

    private static OkHttpClientHelper clientHelper;

    public static OkHttpClientHelper getInstance(){
        if(clientHelper==null){
            synchronized (OkHttpClientHelper.class){
                if(clientHelper==null){
                    clientHelper = new OkHttpClientHelper();
                }
            }
        }
        return clientHelper;
    }


    //获取OKHttpClicent对象
    public OkHttpClient getOkHttpClient(){
//        new CookieJar() {
//            @Override
//            public void saveFromResponse(final HttpUrl url, List<Cookie> cookies) {
//                db.delete("cookie",null,null);
//                for (Cookie cookie:cookies) {
//                    ContentValues cv=new ContentValues();
//                    cv.put("name",cookie.name());
//                    cv.put("url",url.toString());
//                    cv.put("value",cookie.value());
//                    cv.put("domain",cookie.domain());
//                    cv.put("expiresAt",cookie.expiresAt());
//                    db.insert("cookie",cv);
//                }
//            }
//
//            @Override
//            public List<Cookie> loadForRequest(final HttpUrl url) {
//                Cursor cursor=db.query("cookie","where url="+url.toString());
//                List<Cookie> cookies=new ArrayList<Cookie>();
//                do{
//                    cookies.add(new Cookie.Builder()
//                            .name(cursor.getString(1))
//                            .value(cursor.getString(3))
//                            .domain(cursor.getString(4))
//                            .expiresAt(cursor.getLong(5))
//                            .build());
//                }while (cursor.moveToNext());
//                return cookies;
//            }
//        }
        if(mClient ==null) {
            mClient = new OkHttpClient.Builder()
                    .connectTimeout(TIMEOUT, TimeUnit.SECONDS)
                    .readTimeout(TIMEOUT, TimeUnit.SECONDS)
                    .writeTimeout(TIMEOUT, TimeUnit.SECONDS)
                    .cookieJar(cookieJar)
                    .build();
        }
        return mClient;

    }

}
